//
//  MTClubAvatorGroupView.swift
//  MT
//
//  Created by ooops on 2019/12/25.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit

class MTClubAvatorGroupView: UIView {
    
    var imgList: [UIImage]? {
        didSet {
            if let count = imgList?.count {
                switch count {
                case 1:
                    topLeftAvator.snp.remakeConstraints { (make) in
                        make.edges.equalToSuperview()
                    }
                    topLeftAvator.isHidden = false
                    topRightAvator.isHidden = true
                    bottomLeftAvator.isHidden = true
                    bottomRightAvator.isHidden = true
                    if let img = imgList?.first {
                        //topLeftAvator.kf.setImage(with: URL(string: img))
                        topLeftAvator.image = img
                    } else {
                        topLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                case 2:
                    topLeftAvator.snp.remakeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.left.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    topRightAvator.snp.remakeConstraints { (make) in
                        make.centerY.equalToSuperview()
                        make.right.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    topLeftAvator.isHidden = false
                    topRightAvator.isHidden = false
                    bottomLeftAvator.isHidden = true
                    bottomRightAvator.isHidden = true
                    if let img = imgList?.first {
                        //topLeftAvator.kf.setImage(with: URL(string: img))
                        topLeftAvator.image = img
                    } else {
                        topLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    if let img = imgList?[1] {
                        topRightAvator.image = img
                    } else {
                        topRightAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                case 3:
                    topLeftAvator.snp.remakeConstraints { (make) in
                        make.centerX.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    bottomLeftAvator.snp.remakeConstraints { (make) in
                        make.left.equalToSuperview()
                        make.bottom.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    bottomRightAvator.snp.remakeConstraints { (make) in
                        make.right.equalToSuperview()
                        make.bottom.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    topLeftAvator.isHidden = false
                    topRightAvator.isHidden = true
                    bottomLeftAvator.isHidden = false
                    bottomRightAvator.isHidden = false
                    
                    if let img = imgList?.first {
                        topLeftAvator.image = img
                    } else {
                        topLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                    if let img = imgList?[1] {
                        bottomLeftAvator.image = img
                    } else {
                        bottomLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                    if let img = imgList?[2] {
                        bottomRightAvator.image = img
                    } else {
                        bottomRightAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                default:
                    topLeftAvator.snp.remakeConstraints { (make) in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    topRightAvator.snp.remakeConstraints { (make) in
                        make.right.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    bottomLeftAvator.snp.remakeConstraints { (make) in
                        make.left.equalToSuperview()
                        make.bottom.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    bottomRightAvator.snp.remakeConstraints { (make) in
                        make.right.equalToSuperview()
                        make.bottom.equalToSuperview()
                        make.width.height.equalToSuperview().multipliedBy(0.5)
                    }
                    topLeftAvator.isHidden = false
                    topRightAvator.isHidden = false
                    bottomLeftAvator.isHidden = false
                    bottomRightAvator.isHidden = false
                    
                    if let img = imgList?.first {
                        topLeftAvator.image = img
                    } else {
                        topLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                    if let img = imgList?[1] {
                        topRightAvator.image = img
                    } else {
                        topRightAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                    if let img = imgList?[2] {
                        bottomLeftAvator.image = img
                    } else {
                        bottomLeftAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                    
                    if let img = imgList?[3] {
                        bottomRightAvator.image = img
                    } else {
                        bottomRightAvator.image = UIImage(named: defaultPlaceholderIcon60x60)
                    }
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    convenience init(frame: CGRect, imgList: [UIImage]?) {
        self.init(frame: frame)
        self.imgList = imgList
    }
    
    private func setupUI() {
        
        addSubview(topLeftAvator)
        addSubview(topRightAvator)
        addSubview(bottomLeftAvator)
        addSubview(bottomRightAvator)
    }
    
    private func makeConstraint() {
        topLeftAvator.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalToSuperview().multipliedBy(0.5)
        }
        topRightAvator.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.top.equalToSuperview()
            make.width.height.equalToSuperview().multipliedBy(0.5)
        }
        bottomLeftAvator.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.height.equalToSuperview().multipliedBy(0.5)
        }
        bottomRightAvator.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.height.equalToSuperview().multipliedBy(0.5)
        }
        topLeftAvator.isHidden = false
        topRightAvator.isHidden = false
        bottomLeftAvator.isHidden = false
        bottomRightAvator.isHidden = false
    }
    
    /// avatorGroup
    lazy var topLeftAvator: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 5
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var topRightAvator: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 5
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var bottomLeftAvator: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 5
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var bottomRightAvator: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 5
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
